//
//  PromptDialog.cpp
//  BayMaxPuzzle
//
//  Created by Jacky on 15/4/3.
//
//

#include "PromptDialog.h"
#include "ui/CocosGUI.h"
#include "Global.h"

USING_NS_CC;
using namespace ui;

PromptDialog* PromptDialog::create(DialogEntry dialogentry)
{
    PromptDialog *pRet = new(std::nothrow) PromptDialog();
    if (pRet && pRet->init(dialogentry))
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

// on "init" you need to initialize your instance
bool PromptDialog::init(DialogEntry dialogentry)
{
    //////////////////////////////
    // 1. super init first
    if (!initWithColor(Color4B(255,255,255,200)))
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    _dialogEntry = dialogentry;
    
    ignoreAnchorPointForPosition(false);
    this->setPosition(Vec2(visibleSize/2));
    this->setScale(0.01);
    
    auto ease = EaseBackOut::create(ScaleTo::create(0.3, 1));
    this->runAction(ease);
    
    std::string text = "";
    switch (_dialogEntry) {
        case DialogEntry::RETURN :
            text="确定要返回关卡吗？";
            break;
        case DialogEntry::RESTART :
            text="确定要重置游戏吗？";
            break;
        default:
            break;
    }
    
    auto label = Label::createWithSystemFont(text, "Arial", 32);
    label->setTextColor(Color4B::BLACK);
    label->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2+100));
    this->addChild(label);
    initButtons();
    initTouchEventlistener();
    
    return true;
}

void PromptDialog::initTouchEventlistener()
{
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [=](Touch *touch,Event *event){
        return false;
    };
    
    listener->setSwallowTouches(true);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void PromptDialog::initButtons()
{
    Size visibleSize = Director::getInstance()->getVisibleSize();
    auto buttonYES = Button::create("origin_normal.png", "origin_press.png");
    buttonYES->setAnchorPoint(Vec2(1, 0.5));
    buttonYES->setTitleText("确定");
    buttonYES->setTitleFontSize(25);
    buttonYES->setTitleColor(yes_color);
    buttonYES->setPressedActionEnabled(true);
    buttonYES->setPosition(Vec2(visibleSize.width/2-50, visibleSize.height/2-50));
    buttonYES->addClickEventListener([=](Ref*){
        this->onButtonYES();
    });
    this->addChild(buttonYES);
    
    auto buttonNO = Button::create("return_normal.png", "return_press.png");
    buttonNO->setAnchorPoint(Vec2(0, 0.5));
    buttonNO->setTitleText("取消");
    buttonNO->setTitleFontSize(25);
    buttonNO->setTitleColor(no_color);
    buttonNO->setPressedActionEnabled(true);
    buttonNO->setPosition(Vec2(visibleSize.width/2+50, visibleSize.height/2-50));
    buttonNO->addClickEventListener([=](Ref*){
        this->onButtonNO();
    });
    this->addChild(buttonNO);

}

void PromptDialog::destroy()
{
    auto ease = EaseBackIn::create(ScaleTo::create(0.3, 0.01));
    auto callfunc = CallFunc::create([=](){
        this->removeFromParent();
    });
    this->runAction(Sequence::create(ease, callfunc, nullptr));
}

void PromptDialog::onButtonYES()
{
    AUDIOENGINE_PLAY_BUTTON_FX;
    switch (_dialogEntry) {
        case DialogEntry::RETURN :
            {
                EventCustom event("game_return");
                _eventDispatcher->dispatchEvent(&event);
            }
            break;
        case DialogEntry::RESTART :
            {
                EventCustom event("game_restart");
                _eventDispatcher->dispatchEvent(&event);
                this->destroy();
            }
            break;
        default:
            break;
    }
}

void PromptDialog::onButtonNO()
{
    AUDIOENGINE_PLAY_BUTTON_FX;
    EventCustom event("game_cancel");
    _eventDispatcher->dispatchEvent(&event);
    this->destroy();
}